﻿package kit4j.view.cs.swing.component;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import kit4j.view.cs.component.BackgroundConstraints;
import kit4j.view.cs.component.FullComponent;
import kit4j.view.cs.component.FullComponentHelper;
import kit4j.view.cs.swing.panel.JFullPanel;

/**
 * 实现了FullComponent接口的继承自JLabel的类型。
 * 
 * @author MiniKnife
 * 
 */
public class JFullLabel extends JLabel implements FullComponent {

	private static final long serialVersionUID = 1L;

	public JFullLabel() {
		super();
	}

	public JFullLabel(String text) {
		super(text);
	}

	public JFullLabel(Icon image) {
		super(image);
	}

	public JFullLabel(String text, int horizontalAlignment) {
		super(text, horizontalAlignment);
	}

	public JFullLabel(Icon image, int horizontalAlignment) {
		super(image, horizontalAlignment);
	}

	public JFullLabel(String text, Icon icon, int horizontalAlignment) {
		super(text, icon, horizontalAlignment);
	}

	private FullComponentHelper fullComponentHelper = new FullComponentHelper(this);

	@Override
	public void setBackgroundImage(Image img) {
		this.fullComponentHelper.setBackgroundImage(img);
	}

	@Override
	public Image getBackgroundImage() {
		return this.fullComponentHelper.getBackgroundImage();
	}

	@Override
	public void paintComponent(Graphics g) {
		this.fullComponentHelper.paintBackgroundImage(g);

		super.paintComponent(g);
	}

	@Override
	public BackgroundConstraints getBackgroundConstraints() {
		return this.fullComponentHelper.getBackgroundConstraints();
	}

	@Override
	public void setBackgroundConstraints(BackgroundConstraints bgc) {
		this.fullComponentHelper.setBackgroundConstraints(bgc);
	}

	@Override
	public void setAutoGenerateComponentsBackgroundImage(boolean autoGenerateComponentsBackgroundImage) {
		this.fullComponentHelper.setAutoGenerateComponentsBackgroundImage(autoGenerateComponentsBackgroundImage);
	}

	@Override
	public boolean isAutoGenerateComponentsBackgroundImage() {
		return this.fullComponentHelper.isAutoGenerateComponentsBackgroundImage();
	}

	public static class Test {
		public static void main(String[] args) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					JFrame frame = new JFrame("test");
					JFullPanel panel = new JFullPanel();
					MediaTracker tracker = new MediaTracker(panel);
					Image bgImage = new ImageIcon("F:/1.jpg").getImage();
					tracker.addImage(bgImage, 0);
					panel.setAutoGenerateComponentsBackgroundImage(true);
					panel.setBackgroundImage(bgImage);
					panel.setBackgroundConstraints(new BackgroundConstraints(BackgroundConstraints.CENTER, 100, 10,
							300, 400, BackgroundConstraints.HORIZONTAL, BackgroundConstraints.VERTICAL));

					JFullLabel lb1 = new JFullLabel("1111111111111111");
					JFullLabel lb2 = new JFullLabel("222222222222222");
					JLabel lb3 = new JLabel("33333334");
					JFullLabel lb4 = new JFullLabel("<html><sup>HTML</sup> <sub><em>Button</em></sub><br>"
							+ "<font color=\"#FF0080\"><u>Multi-line</u></font>");

					JFullButton btn1 = new JFullButton("55555555");
					JFullButton btn2 = new JFullButton("<html><sup>HTML</sup> <sub><em>Button</em></sub><br>"
							+ "<font color=\"#FF0080\"><u>Multi-line</u></font>");
					// btn1.setRolloverEnabled(true);
					// btn1.setRolloverIcon(new ImageIcon("F:/1.jpg"));

					lb1.setForeground(Color.RED);
					lb2.setForeground(Color.RED);
					lb3.setForeground(Color.RED);
					lb4.setForeground(Color.RED);
					btn1.setForeground(Color.RED);
					btn2.setForeground(Color.RED);

					panel.setLayout(new GridLayout(3, 3));
					panel.add(lb1);
					panel.add(lb2);
					panel.add(lb3);
					panel.add(lb4);
					panel.add(btn1);
					panel.add(btn2);

					try {
						tracker.waitForAll();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

					frame.add(panel);

					frame.setSize(300, 300);
					frame.setLocationRelativeTo(null);

					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

					frame.setVisible(true);

					// for(Component com : panel.getComponents()) {
					// System.out.println(com);
					// }
					// System.out.println("lb1");
					// for(Component com : lb1.getComponents()) {
					// System.out.println(com);
					// }
					// System.out.println("lb3");
					// for(Component com : lb3.getComponents()) {
					// System.out.println(com);
					// }
				}
			});
		}
	}

}
